import axios from 'axios'
import type {  AxiosInstance } from 'axios'
import type  { HYRequestConfig } from './type'


class HYRequest {
    instance: AxiosInstance

    // request实例 => axios的实例
    constructor(config: HYRequestConfig) {
        this.instance = axios.create(config)

        // 每个instance实例都需要加拦截器
        this.instance.interceptors.request.use(
            (config) => {
              return config
            },
            (err) => {
              return err
            }
        )
        this.instance.interceptors.response.use(
            (res) => {
                return res.data
            },
            (err) => {
                return err
            }

        )

        this.instance.interceptors.request.use(config.interceptors?.requestFailureFn, config.interceptors?.requestFailureFn)
        this.instance.interceptors.response.use(config.interceptors?.responseSuccessFn,config.interceptors?.responseFailureFn)

    }

    // 封装网络请求方法
    request<T = any>(config: HYRequestConfig<T>) {
        // 单次请求的成功拦截
        if(config.interceptors?.requestSuccessFn) {
            config = config.interceptors.requestSuccessFn(config)
        }

        // 返回promise

        return new Promise<T>((resolve, reject) => {
            this.instance.request<any, T>(config).then((res) => {
                // 单词响应的成功拦截处理
                if(config.interceptors?.responseSuccessFn) {
                    res = config.interceptors.responseSuccessFn(res)
                }
                resolve(res)
            }).catch((err) => {
                reject(err)
            })
        })
    }

    get<T = any>(config: HYRequestConfig<T>) {
        return this.request({...config, method: 'GET'})
    }

    post<T = any>(config: HYRequestConfig<T>) {
        return this.request({...config, method: 'POST'})
    }
    delete<T = any>(config: HYRequestConfig<T>) {
        return this.request({...config, method: 'DELETE'})
    }
    patch<T = any>(config: HYRequestConfig<T>) {
        return this.request({...config, method: 'PATCH'})
    }
}

export default HYRequest